<?php 
session_start();
/*if($REQUEST_METHOD=='POST'){
	header('Expires: ' . gmdate("D, d M Y H:i:s", time()+1000) . ' GMT');
	header('Cache-Control: public,must-revalidate');header("Pragma: no-cache");
}*/
require_once("config.php");
require_once("xtpl.php");

///
function getFirstChar($str, $char){
	return $result = substr($str, 0, strpos($str, $char));
}
///
function codau2kodau( $str){
	$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/",  'a', $str);
	$str = preg_replace ("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str );
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/" , 'i', $str);
	$str  = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o' , $str);
	$str = preg_replace ("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str );
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/" , 'y', $str);
	$str =  preg_replace("/(đ)/", 'd', $str );

	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/" , 'A', $str);
	$str  = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E' , $str);
	$str = preg_replace( "/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str  = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/",  'O', $str);
	$str = preg_replace ("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str );
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/" , 'Y', $str);
	$str =  preg_replace("/(Đ)/", 'D', $str );
	$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
	$str = str_replace("?", "", str_replace("&*#39;","",$str));
	$str = str_replace("!", "", str_replace("&*#39;","",$str));
	$str = str_replace(".", "-", str_replace("&*#39;","",$str));
	$str = str_replace(",", "", str_replace("&*#39;","",$str));
	$str = str_replace(":", "", str_replace("&*#39;","",$str));
	$str = str_replace("–", "", str_replace("&*#39;","",$str));
	return  htmlspecialchars($str);
}

///
function cutString($str,$maxlen)
{
	if(strlen($str)>$maxlen)
	{
		$newstr=substr($str,0,$maxlen);
		while(' '!=substr($newstr,-1,1))
		{
			$maxlen--;
			$newstr=substr($newstr,0,$maxlen);
		}
		return $newstr."...";
	}
	else
		return $str;
}
///
function strip_special($str){
	return eregi_replace("([~@?=%&!\'\"<>]+)","",$str);		
}
///
function formatData($str){
	if (!get_magic_quotes_gpc()) {
		return addslashes($str);
	} else {
		return $str;
	}
}
///
function check_sign($str) //kiem tra xem phai la khong dau hay khong?
{
	for($i=0;$i<strlen($str);$i++)
	{
		$ord=ord($str[$i]);
		if(($ord==32) || ($ord==35) || ($ord==37) || ($ord==38) || ($ord==39) || ($ord>=48 and $ord<=57) || ($ord>=65 and $ord<=90) || ($ord>=97 and $ord<=122)){//0-9 a-z A - Z
		}else{
			return true;
		}
	}
	return false;
}
///
function pagenavigator($page, $row_total, $page_size, $pagegroup_size, $class, $url) {
	$andpage=(strrpos ($url,"?")===false)?"?page=":"&page=";
	
	//$andpage = "/trang-";
	$page_total = floor(($row_total-1)/$page_size)+1;
	if(!$page || $page > $page_total || $page < 1)
		$page = 1;
	$group = floor(($page-1)/$pagegroup_size)+1;
	$start_page = (($group-1)*$pagegroup_size)+1;
	$end_page = $start_page+$pagegroup_size-1;
	if ($end_page>$page_total) 
		$end_page = $page_total;
	if ($page_total>1) {
		//$str='<div style="vertical-align:middle">';
		if ($end_page>$pagegroup_size)
			$start_group = $pagegroup_size;
		else
			$start_group = 0;
		if ($group>1)
			$str.= '<a href="'.$url.$andpage.($start_page-$pagegroup_size).$ext.'" class="page_previous"></a>';
		for ( $i=$start_page ; $i <= $end_page ;$i++)
		{
			if ($i == $page)
				$str.= '<a href="#" class="page_no_selected"><div style="padding-top:0px;">'.$i.'</div></a>';
			else
				$str.= '<a href="'.$url.$andpage.$i.$ext.'" class="'.$class.'"><div style="padding-top:0px;">'.$i.'</div></a> ';
		}
		if ($page_total-$end_page>$pagegroup_size)
			$end_group = $pagegroup_size;
		else
			$end_group = $page_total-$end_page;
		if ($end_page<$page_total)
			$str.= '<a href="'.$url.$andpage.($end_page+1).$ext.'" class="page_next"></a>';
		//$str.='</div>';
		return $str;
	}	
}
///
function _POST($value){
	global $_GET, $_POST, $HTTP_POST_VARS,$HTTP_GET_VARS;
	if (isset($_POST["$value"]))
		return $_POST["$value"];
	elseif (isset($HTTP_POST_VARS["$value"]))
	    return $HTTP_POST_VARS["$value"];
	elseif (isset($_GET["$value"]))
	    return $_GET["$value"];
	elseif (isset($HTTP_GET_VARS["$value"]))
	    return $HTTP_GET_VARS["$value"];
	else
	    return ;
}
function READ_SESSION($session_name)
{
    return $_SESSION[$session_name];
}
function WRITE_SESSION($session_name, $session_value)
{
	$_SESSION[$session_name]=$session_value;
}
function Printdate($strdate, $showtime=false, $vn='e') {
	$datetime= strtotime($strdate);
	if($vn=='v'){
		switch(date("w", $datetime)) {
			case 0:	 $ngay .= "Ch&#7911; Nh&#7853;t"; break;
			case 1:	 $ngay .="Th&#7913; Hai"; break;
			case 2:	 $ngay .="Th&#7913; Ba"; break;
			case 3:	 $ngay .="Th&#7913; T&#432;"; break;
			case 4:	 $ngay .="Th&#7913; N&#259;m"; break;
			case 5:	 $ngay .="Th&#7913; S&#225;u"; break;
			case 6:	 $ngay .="Th&#7913; B&#7849;y"; break;
		}
	}else{
		switch(date("w", $datetime)) {
			case 0:	 $ngay .= "Sunday"; break;
			case 1:	 $ngay .="Monday"; break;
			case 2:	 $ngay .="Tuesday"; break;
			case 3:	 $ngay .="Wednesday"; break;
			case 4:	 $ngay .="Thursday"; break;
			case 5:	 $ngay .="Friday"; break;
			case 6:	 $ngay .="Saturday"; break;
		}
	}
	
	if($showtime){
		$ngay .= ", ".date("d/m/Y", $datetime).", ".date("H:i", $datetime);
	}else{
		$ngay .= ", ".date("d/m/Y", $datetime);
	}
 	return $ngay;
}
?>